/****************************************************************************************
 ** Server is an application to manage several clients inside a thread.
 ** Copyright (C) 2013  Francesc Martinez <es.linkedin.com/in/cescmm/en>
 **
 ** This library is free software; you can redistribute it and/or
 ** modify it under the terms of the GNU Lesser General Public
 ** License as published by the Free Software Foundation; either
 ** version 2.1 of the License, or (at your option) any later version.
 **
 ** This library is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 ** Lesser General Public License for more details.
 **
 ** You should have received a copy of the GNU Lesser General Public
 ** License along with this library; if not, write to the Free Software
 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ***************************************************************************************/
 
#ifndef CLIENTTHREAD_H
#define CLIENTTHREAD_H

#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QTcpSocket>


class ClientThread : public QThread
{
Q_OBJECT

signals:
	void receivedMessage(const QByteArray &msg);
	void wasConnected();
	void wasDisconnected();
	void connectionError(const QString &err);

public:
	explicit ClientThread(QObject *parent = 0);
	void setServer(QString ip, int port);
	void run();
	void closeConnection();
	void sendMessage(const QByteArray &msg);
	QTcpSocket* getClient() const { return client; }

public slots:
	void readyRead();
	void disconnected();
	void onError(QAbstractSocket::SocketError error);

private:
	QTcpSocket *client;
	bool quit;
	QList<QByteArray> msgToWrite;
	QString _ip;
	int _port;
};

#endif // CLIENTTHREAD_H
